package lib.weixininteract;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lib.Dao.UserDao;
import lib.com.qq.weixin.mp.aes.AesException;
import lib.com.qq.weixin.mp.aes.SHA1;
import lib.pay.HttpRequest;
import lib.wxpay.sdk.WXPayUtil;
import org.apache.commons.io.IOUtils;


/**
 * Servlet implementation class test
 */
@WebServlet("/gettoken")
public class GetTokenFromWX extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetTokenFromWX() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub

        String signature = request.getParameter("signature");
        String timestamp = request.getParameter("timestamp");
        String nonce = request.getParameter("nonce");
        String echostr = request.getParameter("echostr");
        String token = "lipeiyuan";//这里填基本配置中的token
        String jiami = "";
        try {
            jiami = SHA1.getSHA1(token, timestamp, nonce, "");//这里是对三个参数进行加密
        } catch (AesException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        if (jiami.equals(signature))
            response.getWriter().print(echostr);

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        UserDao userDao=new UserDao();
        InputStream is = null;
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            is = request.getInputStream();//获取请求的流信息(这里是微信发的xml格式所有只能使用流来读)
            String xml = IOUtils.toString(is, "utf-8");
            Map<String, String> notifyMap = WXPayUtil.xmlToMap(xml);//将微信发的xml转map

            if (notifyMap.get("Event").equals("subscribe")) {
                if (notifyMap.get("EventKey")!=null) {
                    String openid = notifyMap.get("EventKey").replace("qrscene_", "");

                     /*以下是自己的业务处理-
                     *
                     */
                    userDao.preusercountadd(openid);
                    Map<String, String> paraMap = new HashMap<String, String>();
                    paraMap.put("ToUserName",notifyMap.get("FromUserName"));
                    paraMap.put("FromUserName",notifyMap.get("ToUserName"));
                    paraMap.put("CreateTime",notifyMap.get("CreateTime"));
                    paraMap.put("MsgType","text");
                    paraMap.put("Content","欢迎关注云逸抢课助手，本系统可实时监控选课系统课程余量。点击下方菜单即可进入预约系统。当你不想在接收某节课当余量提醒请在快捷菜单进入订单页面进行退订.演示视频网址：https://www.bilibili.com/video/av80100558");
                    String responseXml = WXPayUtil.mapToXml(paraMap);//将所有参数(map)转xml格式
                    out.println(responseXml);
                }
            }


        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

